//
//  UpdatePwdViewController.m
//  TengYunWaystation
//
//  Created by Johnny on 15/5/9.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "UpdatePwdViewController.h"
#import "SocketRequest.h"
#import "Singleton.h"
#import "TSHeader.h"

@interface UpdatePwdViewController ()<SocketRequestDelegate> {
    SocketRequest *_socketRequest;
}

@end

@implementation UpdatePwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"修改密码";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _inputView.layer.borderColor = LineColor.CGColor;
    _inputView.layer.borderWidth = 0.5;
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(74, 43.5, 260, 0.5)];
    line.backgroundColor = LineColor;
    [_inputView addSubview:line];
}

- (void)updateSocket {
    [ToastActivity makeActivity:@"密码修改中..." InView:self.view];
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STU3"]];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.userId] length:64];
    [mutableData appendBytes:[SocketMethod checkLegal:_oldpwdTextField.text] length:128];
    [mutableData appendBytes:[SocketMethod checkLegal:_newpwdTextField.text] length:128];
    [_socketRequest addSocketData:mutableData SocketTag:1];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)logout {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LogoutSuccess" object:nil];
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    if ([code isEqualToString:SuccessSocket]) {
        [ToastActivity makeSuccess:@"密码修改成功" InView:self.view];
        [self performSelector:@selector(logout) withObject:nil afterDelay:1];
    } else {
        [ToastActivity makeError:msg InView:self.view];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity makeError:msg InView:self.view];
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == _oldpwdTextField) {
        [_newpwdTextField becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    return YES;
}

#pragma mark
- (IBAction)isShowPasswordAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    _newpwdTextField.secureTextEntry = !_newpwdTextField.secureTextEntry;
}

- (IBAction)sureUpdatePassword:(id)sender {
    [self.view endEditing:YES];
    if (_oldpwdTextField.text.length <= 0) {
        [DSToast showText:@"请输入旧密码" InView:self.view];
    } else if (_newpwdTextField.text.length <= 0) {
        [DSToast showText:@"请输入新密码" InView:self.view];
    } else if (_newpwdTextField.text.length < 6) {
        [DSToast showText:@"密码至少六位" InView:self.view];
    } else {
        [self updateSocket];
    }
}

- (void)dealloc {
    _socketRequest = nil;
}

@end
